package com.zjdiepu.www.biz.cw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.cw.CwRebateMapper;
import com.zjdiepu.www.ibiz.cw.ICwRebateService;
import com.zjdiepu.www.model.cw.dto.CwRebate;
import com.zjdiepu.www.model.cw.ext.CwRebateExt;

/**
 * 返利和返利调整Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年07月19日 15时47分<br/>
 * @since 1.0<br/>
 */
@Service
public class CwRebateServiceImpl extends BaseService implements ICwRebateService {
	public static final Logger logger = LoggerFactory.getLogger(CwRebateServiceImpl.class);

	@Autowired
	private CwRebateMapper cwRebateMapper;
	
	@Override
	public void saveUpdateAndDelete(CwRebate cwRebate, OperateType operType) {
		switch (operType) {
		case add:
			cwRebate.initInsertData(cwRebate.getModifier(), cwRebate.getModifierName());
			cwRebateMapper.insert(cwRebate);
			break;
		case edit:
			cwRebate.initUpdateData(cwRebate.getModifier(), cwRebate.getModifierName());
			// 影响的记录数
			int updateCount = cwRebateMapper.updateByPrimaryKeySelective(cwRebate);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			cwRebate.initDeleteData(cwRebate.getModifier(), cwRebate.getModifierName());
			// 影响的记录数
			int deleteCount = cwRebateMapper.updateByPrimaryKeySelective(cwRebate);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<CwRebate> selectByExt(CwRebateExt cwRebateExt) {
		return cwRebateMapper.selectByExt(cwRebateExt);
	}
	
	@Override
	public int countByExt(CwRebateExt cwRebateExt){
		return cwRebateMapper.countByExt(cwRebateExt);
	}
	
	@Override
	public CwRebate selectById(String id) {
		return cwRebateMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			cwRebateMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(CwRebate cwRebate) {
		return cwRebateMapper.countRefined(cwRebate);
	}

	@Override
	public CwRebate selectRefined(CwRebate cwRebate) {
		return cwRebateMapper.selectRefined(cwRebate);
	}
	
	@Override
	public int deletedRefined(CwRebate cwRebate) {
		return cwRebateMapper.deletedRefined(cwRebate);
	}

	@Override
	public boolean repeat(CwRebate cwRebate) {
		int count = countRefined(cwRebate);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(cwRebate.getId())) {
			return true;
		}
		CwRebate dbItem = selectRefined(cwRebate);
		if(null == dbItem) {
			return false;
		}
		return !cwRebate.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return cwRebateMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<CwRebate> cwRebates) {
		return cwRebateMapper.insertBatch(cwRebates);
	}
}
